#include "qmemerydataproperty.h"

#include "qmemerydataeditor.h"

#include "../../libs/sharedLib/qcommonstruct.h"
#include "../../libs/sharedLib/qbtneditorlabel.h"


QMemeryDataProperty::QMemeryDataProperty(SHAREDLIB::QAbstractProperty *parent):
    SHAREDLIB::QAbstractProperty(parent)
{

}

QString QMemeryDataProperty::valueText()
{
    MemeryValueItems c=getValue().value<MemeryValueItems>();
    if(c.size()==0)
    {
        return "None";
    }
    else
    {
        return QString("count:%1").arg(c.size());
    }
}

QWidget* QMemeryDataProperty::getEditor()
{
    if(attribute("editabled").toBool())
    {
        SHAREDLIB::QBtnEditorLabel *e=new SHAREDLIB::QBtnEditorLabel(this);
        connect(e,SIGNAL(btnClicked()),this,SLOT(btnClicked()));
        return e;
    }
    return NULL;
}

void QMemeryDataProperty::valueXML(QXmlStreamWriter *w)
{
    MemeryValueItems c=getValue().value<MemeryValueItems>();
    foreach(tagMemeryValueInfo item,c)
    {
        w->writeStartElement("item");

        w->writeAttribute("property",item.m_propertyName);
        w->writeAttribute("group",item.m_valueGroup);
        w->writeAttribute("value",item.m_valueName);
        w->writeAttribute("script",item.m_script);

        w->writeEndElement();
    }
}

void QMemeryDataProperty::parserXML(QXmlStreamReader *r)
{
    MemeryValueItems c;
    tagMemeryValueInfo item;

    for(bool finished=false;!finished && !r->hasError();)
    {
        switch(r->readNext())
        {
        case QXmlStreamReader::StartElement:
            if(r->name()=="item")
            {
                item=readItem(r);
                c.append(item);
            }
            break;
        case QXmlStreamReader::EndElement:
            finished=true;
            break;
        default:
            break;
        }
    }
    QVariant v;
    v.setValue<MemeryValueItems>(c);
    setValue(v);
}

tagMemeryValueInfo QMemeryDataProperty::readItem(QXmlStreamReader *r)
{
    tagMemeryValueInfo item;

    QString str;

    foreach(const QXmlStreamAttribute &attribute,r->attributes())
    {
        str=attribute.name().toString();
        if(str=="property")
        {
            item.m_propertyName=attribute.value().toString();
        }
        else if(str=="value")
        {
            item.m_valueName=attribute.value().toString();
        }
        else if(str=="script")
        {
            item.m_script=attribute.value().toString();
        }
        else if(str=="group")
        {
            item.m_valueGroup=attribute.value().toString();
        }
    }

    for(bool finished=false;!finished && !r->hasError();)
    {
        switch(r->readNext())
        {
        case QXmlStreamReader::StartElement:
            break;
        case QXmlStreamReader::EndElement:
            finished=true;
            break;
        default:
            break;
        }
    }
    if(item.m_propertyName=="" || item.m_valueName=="" || item.m_valueGroup=="")
    {
        r->raiseError("Memerydata item error!");
    }
    return item;
}

void QMemeryDataProperty::btnClicked()
{
    QMemeryDataEditor dlg(this);
    dlg.exec();
}
